package com.situ.staff.init;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class UserFilter
 */
//@WebFilter(urlPatterns = {"/web/page/*","/employee","/DepartmentServlet"})
public class UserFilter implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException, ServletException {
		HttpServletRequest req_ = (HttpServletRequest) request;
		String action  = req_.getParameter("action");
		//登陆和注册的放行
		if("reg".equals(action)||"login".equals(action)) {
			chain.doFilter(request, response);
			return;
		}
		//登陆之后的放行
		Object user = req_.getSession().getAttribute("user");
//		System.out.println(user);
		if(user==null) {
			request.getRequestDispatcher("/web/login.jsp").forward(request, response);
		}else 
			chain.doFilter(request, response);
		
	}
}
